package com.heima.minio.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PlaneWar extends JFrame implements ActionListener {
    private Timer timer;
    private JLabel scoreLabel;
    private int score = 0;
    private JPanel gamePanel;
    private Player player;
    private Enemy enemy;

    public PlaneWar() {
        super("Plane War");

        // 设置窗口大小
        setSize(400, 600);

        // 创建计时器，每隔10毫秒刷新游戏画面
        timer = new Timer(10, this);

        // 创建得分标签
        scoreLabel = new JLabel("Score: 0");
        scoreLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        scoreLabel.setForeground(Color.WHITE);

        // 创建游戏面板
        gamePanel = new JPanel();
        gamePanel.setLayout(null);
        gamePanel.setBackground(Color.BLACK);
        gamePanel.add(scoreLabel);

        // 设置得分标签的位置
        scoreLabel.setBounds(10, 10, 100, 30);

        // 创建玩家飞机
        player = new Player();

        // 创建敌机
        enemy = new Enemy();

        // 将玩家飞机和敌机添加到游戏面板
        gamePanel.add(player);
        gamePanel.add(enemy);

        // 将游戏面板添加到窗口
        add(gamePanel);

        // 显示窗口
        setVisible(true);

        // 开始游戏
        timer.start();
    }

    // 实现ActionListener接口的方法，处理计时器事件
    public void actionPerformed(ActionEvent e) {
        // 更新玩家飞机和敌机的位置
        player.update();
        enemy.update();

        // 检测玩家飞机和敌机是否相撞
        if (player.getBounds().intersects(enemy.getBounds())) {
            // 相撞后，玩家飞机爆炸，游戏结束
            player.explode();
            timer.stop();
            JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score);
        }

        // 检测玩家飞机是否击中敌机
        if (player.isShooting() && player.getBullet().getBounds().intersects(enemy.getBounds())) {
            // 击中后，敌机爆炸，得分增加
            enemy.explode();
            score += 10;
            scoreLabel.setText("Score: " + score);
        }
    }

    // 玩家飞机类
    private class Player extends JLabel implements KeyListener {
        private int x = 175;
        private int y = 500;
        private boolean shooting = false;
        private Bullet bullet;

        public Player() {
            super(new ImageIcon("player.png"));

            // 设置玩家飞机的位置
            setBounds(x, y, 50, 50);

            // 创建子弹
            bullet = new Bullet(x + 20, y - 20);

            // 监听键盘事件
            addKeyListener(this);
            setFocusable(true);
        }

        // 实现KeyListener接口的方法，处理键盘事件
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_LEFT) {
                x -= 5;
            } else if (keyCode == KeyEvent.VK_RIGHT) {
                x += 5;
            } else if (keyCode == KeyEvent.VK_SPACE) {
                shooting = true;
            }
            setBounds(x, y, 50, 50);
        }

        public void keyReleased(KeyEvent e) {}

        public void keyTyped(KeyEvent e) {}

        // 更新玩家飞机和子弹的位置
        public void update() {
            bullet.update();
            if (shooting) {
                gamePanel.add(bullet);
                shooting = false;
            }
        }

        // 获取子弹对象
        public Bullet getBullet() {
            return bullet;
        }

        // 判断是否正在发射子弹
        public boolean isShooting() {
            return bullet.getParent() != null;
        }

        // 玩家飞机爆炸
        public void explode() {
            gamePanel.remove(this);
            gamePanel.repaint();
        }
    }

    // 敌机类
    private class Enemy extends JLabel {
        private int x = 175;
        private int y = 50;
        private int dx = 1;
        private int dy = 1;
        private boolean exploded = false;

        public Enemy() {
            super(new ImageIcon("enemy.png"));

            // 设置敌机的位置
            setBounds(x, y, 50, 50);
        }

        // 更新敌机的位置
        public void update() {
            if (!exploded) {
                x += dx;
                y += dy;
                if (x < 0 || x > 350) {
                    dx = -dx;
                }
                if (y < 0 || y > 500) {
                    dy = -dy;
                }
                setBounds(x, y, 50, 50);
            }
        }

        // 敌机爆炸
        public void explode() {
            if (!exploded) {
                gamePanel.remove(this);
                gamePanel.repaint();
                exploded = true;
            }
        }
    }

    // 子弹类
    private class Bullet extends JLabel {
        private int x;
        private int y;

        public Bullet(int x, int y) {
            super(new ImageIcon("bullet.png"));

            // 设置子弹的位置
            this.x = x;
            this.y = y;
            setBounds(x, y, 10, 20);
        }

        // 更新子弹的位置
        public void update() {
            y -= 5;
            setBounds(x, y, 10, 20);
            if (y < 0) {
                gamePanel.remove(this);
            }
        }
    }

    public static void main(String[] args) {
        new PlaneWar();
    }
}
